<?php

require PATH_MODELS . 'MenuModel.class.php';

/**
 * 系统设置类
 */
class SystemPage extends BasePage {

    private $M;
    private $way = '';
    static private $dbase = 'tb_menu';
    static private $dbmenu_list = 'tb_menu_group';
    static private $user_menu = 'tb_usre';

    /**
     * 构造方法
     */
    function __construct() {
        parent::checkUserLogin();
        $this->M = new MenuModel();
        $this->way = isset($_GET['way']) ? trim($_GET['way']) : '';
        parent::getMenuList($this->M);
    }

    /**
     * ajax 取菜单联动数据
     */
    function menuGrade() {
        $pid = $_GET['pid'];
        $tnp = $this->M->getList("tb_menu", 'Fid,FcateId,Fname', 'WHERE  FcateId=' . $pid, 'ORDER BY Fsort');

        $select_data[$pid] = $tnp;
        if ($pid == 0) {
            exit;
        }
        echo '<option value="0">' . "请选择下级" . '</option>';
        foreach ($select_data[$pid] as $ls) {
            echo '<option value="' . $ls['Fid'] . '">' . $ls['Fname'] . '</option>';
        }
    }

    /**
     * ajax 修改锁定菜单联动数据
     */
    function menuLock() {
        $pid = $_GET['pid'];
        $sid = $_GET['sid'];
        $tnp = $this->M->getList("tb_menu", 'Fid,FcateId,Fname,FcateIds ', 'WHERE  FcateId=' . $pid, 'ORDER BY Fsort');
        $select_data[$pid] = $tnp;
        if ($pid == 0) {
            exit;
        }

        echo '<option value="0">' . "请选择下级" . '</option>';
        foreach ($select_data[$pid] as $ls) {
            $sclect = '';
            if ($ls['Fid'] == $sid) {
                $sclect.= "selected=selected";
            }
            echo '<option value="' . $ls['Fid'] . '"' . $sclect . '>' . $ls['Fname'] . '</option>';
        }
    }

    function menuInfo() {
        //添加后台菜单
        if ($this->way == 'A') {
            if (!$_POST) {
                // 取一级后台菜单
                $fist_menu = $this->M->getList(self::$dbase, 'Fid,Fname', 'WHERE Flevel = 1');
                include PATH_VIEWS . 'system/menu/add.php';
            } else {
                $cate_id_arr = $_POST['FcateIds'];
                array_unshift($cate_id_arr, '0');
                $cate_id_arr = array_unique($cate_id_arr);
                $cate_ids = implode(',', $cate_id_arr);
                $data = array(
                    'FcateIds' => $cate_ids,
                    'FcateId' => isset($cate_id_arr) ? end($cate_id_arr) : 0,
                    'Fname' => isset($_POST['Fname']) ? trim($_POST['Fname']) : '',
                    'Fclass' => isset($_POST['Fclass']) ? trim($_POST['Fclass']) : '',
                    'Ffunction' => isset($_POST['Ffunction']) ? trim($_POST['Ffunction']) : '',
                    'Fway' => isset($_POST['Fway']) ? trim($_POST['Fway']) : '',
                    'Fsort' => isset($_POST['Fsort']) ? intval($_POST['Fsort']) : 0,
                    'Flevel' => count($cate_id_arr),
                    'Fshow' => isset($_POST['Fshow']) ? intval($_POST['Fshow']) : 1,
                );
                $rs = $this->M->addMenu(self::$dbase, $data);
                if ($rs > 0) {
                    parent::getMsg('right', '操作成功', URL);
                } else {
                    parent::getMsg('wrong', '操作失败', URL);
                }
            }
        }
        //后天菜单列表
        if ($this->way == 'S') {
            //分页参数设置
            //当前页码
            $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
            //每页条数
            $pagesize = 10;
            $page = ($page < 1) ? 1 : $page;
            //起始条数
            $start = ($page - 1) * $pagesize;
            //查询关键字
            $keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';
            $words_arr = BaseYsp::wordsFilter($keywords);
            $where = "WHERE Fid>=1";
            if (!empty($keywords)) {
                foreach ($words_arr as $wd) {
                    $where .= " AND (Fname LIKE '%$wd%' OR Fclass LIKE '%$wd%')";
                }
            }
            $where = $keywords ? "WHERE Fname LIKE '%$keywords%' OR Fclass LIKE '%$keywords%' OR Ffunction LIKE '%$keywords%'" : '';

            //取总记录数
            $total = $this->M->getTotal(self::$dbase, $where);
            $info_list = $this->M->getList(self::$dbase, '*', $where, 'ORDER BY FcateId,Flevel,Fsort', "LIMIT $start,$pagesize");
            if ($total > 0) {
                $cate_ids = 0;
                foreach ($info_list as $ls) {
                    $cate_ids .= ',' . $ls['FcateIds'];
                }
                //取所有菜单，构造数组
                $categroy = $this->M->getList(self::$dbase, 'Fid,Fname', "WHERE Fid IN ($cate_ids)");
                $category_arr[0] = '顶级';
                foreach ($categroy as $ls) {
                    $category_arr[$ls['Fid']] = $ls['Fname'];
                }
                $Pager = new ExtendsYspPager();
                $pagerlink = $Pager->getPager($total, $page, $pagesize);
            } else {
                $pagerlink = '对不起，没有相关信息';
            }
            include PATH_VIEWS . 'system/menu/list.php';
        }
        //后台菜单修改
        if ($this->way == 'E') {
            if (!$_POST) {
                // 取当前数据
                $edit = $this->M->getView(self::$dbase, 'Fid');
                // 判断当前的一级菜单ID
                $cate_id_arr = explode(',', $edit['FcateIds']);
                $cate_id = isset($cate_id_arr[1]) ? $cate_id_arr[1] : 0;
                $two_id = end($cate_id_arr);
                // 判断二级菜单ID
                // 取一级菜单
                $menu_name = $this->M->getlist(self::$dbase, 'Fid,Fname', 'WHERE Flevel=1');
                include PATH_VIEWS . 'system/menu/edit.php';
            } else {
                $cate_id_arr = $_POST['FcateIds'];
                array_unshift($cate_id_arr, '0');
                $cate_id_arr = array_unique($cate_id_arr);
                $cate_ids = implode(',', $cate_id_arr);
                $data = array(
                    'FcateIds' => $cate_ids,
                    'FcateId' => isset($cate_id_arr) ? end($cate_id_arr) : 0,
                    'Fname' => isset($_POST['Fname']) ? trim($_POST['Fname']) : '',
                    'Fclass' => isset($_POST['Fclass']) ? trim($_POST['Fclass']) : '',
                    'Ffunction' => isset($_POST['Ffunction']) ? trim($_POST['Ffunction']) : '',
                    'Fway' => isset($_POST['Fway']) ? trim($_POST['Fway']) : '',
                    'Fsort' => isset($_POST['Fsort']) ? intval($_POST['Fsort']) : 0,
                    'Flevel' => count($cate_id_arr),
                    'Fshow' => isset($_POST['Fshow']) ? intval($_POST['Fshow']) : 1,
                );
                $rs = $this->M->Edit(self::$dbase, $data, 'Fid');
                if ($rs !== false) {
                    parent::getMsg('right', '操作成功', URL);
                } else {
                    parent::getMsg('wrong', '操作失败', URL);
                }
            }
        }
        //后台菜单删除
        if ($this->way == 'D') {
            $rs = $this->M->Del(self::$dbase, 'Fid');
            if ($rs !== false) {
                $remark = "[" . BaseYspCookie::getCookie($_COOKIE['admin']['user']) . "]删除一个菜单。";
                $href = str_replace('&way=D', '&way=S', URL);
                parent::getMsg('right', '操作成功', $href);
                exit;
            } else {
                parent::getMsg('wrong', '对不起，操作失败', URL);
                exit;
            }
        }
//        添加菜单分组
        $menu_list = $this->M->getList(self::$dbase, 'Fid,Fname,FcateId');
        $menu_arr = array();
        foreach ($menu_list as $ls) {
            $menu_arr[$ls['FcateId']][$ls['Fid']] = $ls['Fname'];
        }
        if ($this->way == 'AA') {

            if (!$_POST) {
                include PATH_VIEWS . 'system/menu/group_menu.php';
            } else {
                $cate_id_arr = $_POST['FcatIds'];
                array_unshift($cate_id_arr, '0');
                $cate_id_arr = array_unique($cate_id_arr);
                $cate_ids = implode(',', $cate_id_arr);
                $data = array(
                    'FcatIds' => $cate_ids,
                    'Fname' => isset($_POST['Fname']) ? trim($_POST['Fname']) : '',
                    'Fsort' => isset($_POST['Fsort']) ? intval($_POST['Fsort']) : 0,
                );
                $rs = $this->M->addMenu(self::$dbmenu_list, $data);
                if ($rs > 0) {
                    parent::getMsg('right', '操作成功', URL);
                } else {
                    parent::getMsg('wrong', '操作失败', URL);
                }
            }
        }

//        菜单分组列表
        if ($this->way == 'SS') {
            //分页参数设置
            //当前页码
            $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
            //每页条数
            $pagesize = 10;
            $page = ($page < 1) ? 1 : $page;
            //起始条数
            $start = ($page - 1) * $pagesize;
            //查询关键字
            $keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';
            $where = $keywords ? "WHERE Fname LIKE '%$keywords%'" : '';

            //取总记录数
            $total = $this->M->getTotal(self::$dbmenu_list, $where);
            $info_list = $this->M->getList(self::$dbmenu_list, '*', $where, 'ORDER BY Fsort', "LIMIT $start,$pagesize");
            if ($total > 0) {
                $Pager = new ExtendsYspPager();
                $pagerlink = $Pager->getPager($total, $page, $pagesize);
            } else {
                $pagerlink = '对不起，没有相关信息';
            }
            include PATH_VIEWS . 'system/menu/list_menu.php';
        }
//        菜单分组删除
        if ($this->way == 'DD') {
            $rs = $this->M->Del(self::$dbmenu_list, 'Fid');
            if ($rs !== false) {
                $remark = "[" . BaseYspCookie::getCookie($_COOKIE['admin']['user']) . "]删除一个菜单。";
                $href = str_replace('&way=DD', '&way=SS', URL);
                parent::getMsg('right', '操作成功', $href);
                exit;
            } else {
                parent::getMsg('wrong', '对不起，操作失败', URL);
                exit;
            }
        }
//        菜单分组修改
        if ($this->way == 'EE') {
            if (!$_POST) {
                $edit = $this->M->getView(self::$dbmenu_list, 'Fid');
                $cate_id_arr = explode(',', $edit['FcatIds']);
                include PATH_VIEWS . 'system/menu/edit_group.php';
            } else {
                if ($_POST['FcatIds'] != '') {
                    $menu_ids = implode(',', $_POST['FcatIds']);
                }
                $data = array(
                    'FcatIds' => trim($menu_ids, ','),
                    'Fname' => isset($_POST['Fname']) ? trim($_POST['Fname']) : '',
                    'Fsort' => isset($_POST['Fsort']) ? intval($_POST['Fsort']) : 1
                );
                //修改数据
                $rs = $this->M->Edit(self::$dbmenu_list, $data, 'Fid');
                if ($rs !== false) {
                    parent::getMsg('right', '操作成功', URL);
                    exit;
                } else {
                    parent::getMsg('wrong', '操作失败', URL);
                    exit;
                }
            }
        }
    }

    /**
     * 特殊权限类别管理
     */
    function specialPowerCategory() {
//        添加特殊权限类别
        if ($this->way == "A") {
            include PATH_VIEWS . 'system/special_power/add_category.php';
        }
//        特殊权限类别列表
        if ($this->way == "S") {
            include PATH_VIEWS . 'system/special_power/list_category.php';
        }
    }

    /**
     * 特殊权限管理
     */
    function specialPower() {
//        添加特殊权限
        if ($this->way == "A") {
            include PATH_VIEWS . 'system/special_power/add_power.php';
        }
//        特殊权限列表
        if ($this->way == "S") {
            include PATH_VIEWS . 'system/special_power/list_power.php';
        }
    }

    /**
     * 特殊权限分组管理
     */
    function specialPowerGroup() {
//        添加特殊权限分组
        if ($this->way == "G") {
            include PATH_VIEWS . 'system/special_power/add_group.php';
        }
//        特殊权限分组列表
        if ($this->way == "S") {
            include PATH_VIEWS . 'system/special_power/list_group.php';
        }
    }

    /**
     * 账户管理
     */
    function userInfo() {
//        添加账户
        if ($this->way == "A") {
            if (!$_POST) {
                $info_list = $this->M->getList(self::$dbmenu_list, '*', 'ORDER BY Fsort');
                include PATH_VIEWS . 'system/users/add.php';
            } else {
                if ($_POST['Fpassword'] != $_POST['Fpassword1']) {
                    echo '两次输入密码不一致';
                    return FALSE;
                }
                if ($_POST['Fpassword'] == '') {
                    echo '密码不能为空';
                    return FALSE;
                }

                $array_ids = isset($_POST['Fmenu']) ? $_POST['Fmenu'] : array();
                $array_ids = array_unique($array_ids);
                $array_ids = implode(',', $array_ids);
                $power_ids = isset($_POST['Fpower']) ? $_POST['Fpower'] : array();
                $power_ids = array_unique($power_ids);
                $power_ids = implode(',', $power_ids);
                $data = array(
                    'Faccount' => isset($_POST['Faccount']) ? trim($_POST['Faccount']) : '',
                    'Fpassword' => isset($_POST['Fpassword']) ? md5(trim($_POST['Fpassword'])) : '',
                    'Fname' => isset($_POST['Fname']) ? trim($_POST['Fname']) : '',
                    'Fsex' => isset($_POST['Fsex']) ? trim($_POST['Fsex']) : '',
                    'Fmenu' => trim($array_ids),
                    'Fpower' => trim($power_ids),
                    'Fmail' => isset($_POST['Fmail']) ? trim($_POST['Fmail']) : '',
                    'Fqq' => isset($_POST['Fqq']) ? trim($_POST['Fqq']) : '',
                    'Fmobile' => isset($_POST['Fmobile']) ? trim($_POST['Fmobile']) : '',
                    'Ftel' => isset($_POST['Ftel']) ? trim($_POST['Ftel']) : '',
                    'FaddTime' => time()
                );

                $info = $this->M->getOneRow(self::$user_menu, 'Fid', "WHERE Faccount='{$data['Faccount']}'");
                if (isset($info['Fid'])) {
                    echo '对不起，改账号已被注册';
                    return FALSE;
                }
                $user = $this->M->addMenu(self::$user_menu, $data);
                if ($user !== false) {
                    parent::getMsg('right', '操作成功', URL);
                    exit;
                } else {
                    parent::getMsg('wrong', '操作失败', URL);
                    exit;
                }
            }
        }
//        账户列表
        if ($this->way == "S") {
            //分页参数设置
            //当前页码
            $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
            //每页条数
            $pagesize = 10;
            $page = ($page < 1) ? 1 : $page;
            //起始条数
            $start = ($page - 1) * $pagesize;
            //查询关键字
            $keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';
            $where = $keywords ? "WHERE Fname LIKE '%$keywords%'" : '';

            //取总记录数
            $total = $this->M->getTotal(self::$user_menu, $where);
            $info_list = $this->M->getList(self::$user_menu, '*', $where, 'ORDER BY Fid', "LIMIT $start,$pagesize");
            if ($total > 0) {
                $cate_ids = 0;
                foreach ($info_list as $ls) {
                    $cate_ids .= ',' . $ls['Fmenu'];
                }


                $Pager = new ExtendsYspPager();
                $pagerlink = $Pager->getPager($total, $page, $pagesize);
            } else {
                $pagerlink = '对不起，没有相关信息';
            }
            include PATH_VIEWS . 'system/users/list.php';
        }
        //编辑
        if ($this->way == 'E') {
            if (!$_POST) {
                $edit = $this->M->getOneRow('tb_usre', '*');
                include PATH_VIEWS . 'system/users/edit.php';
            } else {
                $menu_id_arr = $_POST['FmenuLevel'];
                $menu_id_arr = array_unique($menu_id_arr);
                $menu_ids = implode(',', $menu_id_arr);
                $user_id_arr = $_POST['FuserLevel'];
                $user_id_arr = array_unique($user_id_arr);
                $user_ids = implode(',', $user_id_arr);
                $data = array(
                    'FmenuLevel' => trim($menu_ids, '0,'),
                    'FuserLevel' => trim($user_ids, '0,'),
                    'Fname' => isset($_POST['Fname']) ? trim($_POST['Fname']) : '',
                    'Faccount' => isset($_POST['Faccount']) ? trim($_POST['Faccount']) : '',
                    'Fsex' => isset($_POST['Fsex']) ? intval($_POST['Fsex']) : 0,
                    'Femail' => isset($_POST['Femail']) ? trim($_POST['Femail']) : '',
                    'Fqq' => isset($_POST['Fqq']) ? trim($_POST['Fqq']) : '',
                    'Fmobile' => isset($_POST['Fmobile']) ? trim($_POST['Fmobile']) : '',
                    'Ftel' => isset($_POST['Ftel']) ? trim($_POST['Ftel']) : '',
                );
                $pwd = isset($_POST['Fpassword']) ? trim($_POST['Fpassword']) : '';
                if (!empty($pwd)) {
                    $data['Fpassword'] = md5($pwd);
                }
                $info = $this->M->getOneRow('tb_admin_users', 'Fid', "WHERE Fid<>" . intval($_GET['primarykey']) . " AND Faccount='{$data['Faccount']}'");
                if (isset($info['Fid'])) {
                    parent::getMsg('wrong', '对不起，该账户已经有人使用，请更新一个新的账户名再试', URL);
                    exit;
                }
                //修改数据
                $rs = $this->M->Edit('tb_user', $data, 'Fid');
                if ($rs !== false) {
                    parent::getMsg('right', '操作成功', URL);
                } else {
                    parent::getMsg('wrong', '操作失败', URL);
                }
            }
        }
        //        菜单分组删除
        if ($this->way == 'D') {
            $rs = $this->M->Del('tb_usre', 'Fid');
            if ($rs !== false) {
                $remark = "[" . $_SESSION['admin']['user'] . "]删除一个菜单。";
                $href = str_replace('&way=D', '&way=S', URL);
                parent::getMsg('right', '操作成功', $href);
                exit;
            } else {
                parent::getMsg('wrong', '对不起，操作失败', URL);
                exit;
            }
        }
    }

    function myInfo() {
        //        个人信息维护
        $user_id = $_SESSION['admin']['id'];
        $edit = $this->M->getOneRow(self::$user_menu, '*', "WHERE Fid=$user_id");
        if ($this->way == "E") {
            if (!$_POST) {
                include PATH_VIEWS . 'system/users/maintenance.php';
            } else {
                $data = array(
                    'Fname' => isset($_POST['Fname']) ? trim($_POST['Fname']) : '',
                    'Fmail' => isset($_POST['Fmail']) ? trim($_POST['Fmail']) : '',
                    'Fqq' => isset($_POST['Fqq']) ? trim($_POST['Fqq']) : '',
                    'Fmobile' => isset($_POST['Fmobile']) ? trim($_POST['Fmobile']) : '',
                    'Ftel' => isset($_POST['Ftel']) ? trim($_POST['Ftel']) : '',
                );
                $pwd = isset($_POST['Fpassword']) ? trim($_POST['Fpassword']) : '';
                //修改数据
                $rs = $this->M->Update(self::$user_menu, $data, "WHERE Fid=" . BaseYspCookie::getCookie($_COOKIE['admin']['id']));
                if ($rs !== false) {
                    parent::getMsg('right', '操作成功', URL);
                    exit;
                } else {
                    parent::getMsg('wrong', '操作失败', URL);
                    exit;
                }
            }
        }
//        修改密码
        if ($this->way == "PASSWORD") {
            if (!$_POST) {
                $user_id = $_SESSION['admin']['id'];
                $edit = $this->M->getOneRow(self::$user_menu, '*', "WHERE Fid=$user_id");
                include PATH_VIEWS . 'system/users/modify.php';
            } else {
                $pwd0 = isset($_POST['pastPassword']) ? md5(trim($_POST['pastPassword'])) : '';
                $pwd1 = isset($_POST['Fpassword']) ? trim($_POST['Fpassword']) : '';
                $pwd2 = isset($_POST['Fpassword1']) ? trim($_POST['Fpassword1']) : '';
                // print_r($pwd0);exit;
                if ($pwd0 != $edit['Fpassword']) {
                    parent::getMsg('wrong', '原密码输入有误', URL);
                    echo '原密码输入有误';
                    exit;
                }
                if ($pwd1 != $pwd2) {
                    parent::getMsg('wrong', '对不起，您两次输入的密码不一致，请重新输入。', URL);
                    echo '两次输入密码不一直';
                    exit;
                }
                $data['Fpassword'] = md5($pwd1);
                //修改数据
                $rs = $this->M->Update(self::$user_menu, $data, 'WHERE Fid=' . $_SESSION['admin']['id']);
                if ($rs !== false) {
                    parent::getMsg('right', '操作成功,请从新登录', 'index.php?C=Login&A=loginOut');
                    exit;
                } else {
                    parent::getMsg('wrong', '操作失败', URL);
                    exit;
                }
            }
        }
    }

}
